package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.activity.OrderConfirmActivity;
import com.avic.rong.userOrder.bean.MemberCardBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinEr on 2019/7/12.
 * Describe:
 */
public class MemberCardListAdapter extends RecyclerView.Adapter<MemberCardListAdapter.ViewHolder> {
    private Context context;
    public int mSelectedPos = -1;//保存当前选中的position
    private ArrayList<MemberCardBean> memberCardList;
    private String memberCardSelect;
    private boolean memberCard = false;
    private String memberID;
    private int tag = -1;

    public String getMemberCardSelect() {
        return memberCardSelect;
    }

    public void setMemberCardSelect(String memberCardSelect) {
        this.memberCardSelect = memberCardSelect;
    }

    public void setmSelectedPos(int mSelectedPos) {
        this.mSelectedPos = mSelectedPos;
    }

    public String getMemberID() {
        return memberID;
    }

    public void setMemberID(String memberID) {
        this.memberID = memberID;
    }

    public MemberCardListAdapter(Context context, ArrayList<MemberCardBean> memberCardList) {
        this.context = context;
        this.memberCardList = memberCardList;

    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.member_card_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        holder.tv_member_title.setText(memberCardList.get(position).getTitle());
        holder.tv_price.setText("¥" + memberCardList.get(position).getBalance());
        holder.tv_member_message.setText(memberCardList.get(position).getTargetName() + " | 有效期至" + memberCardList.get(position).getEndTime());
        double balance = memberCardList.get(position).getBalance();

        if (memberCardList.get(position).getSelect()) {
            holder.iv_check.setSelected(true);
        } else {
            holder.iv_check.setSelected(false);
        }

        holder.iv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ((OrderConfirmActivity)context).memberCardId=memberCardList.get(position).getServiceId() + "";
                ((OrderConfirmActivity)context).memberCardYuE=memberCardList.get(position).getBalance() + "";
//                ((OrderConfirmActivity)context).tv_member_card_dikou.setText("余额¥" +memberCardList.get(position).getBalance());
                ((OrderConfirmActivity)context).vipServiceBean.setBalance(balance);

                if (mSelectedPos == -1) {  // 如果选中
                    memberCardList.get(position).setSelect(true);
                    mSelectedPos = position;
                    ((OrderConfirmActivity)context).memberCardId=memberCardList.get(position).getServiceId()+"";
                } else if (mSelectedPos == position) {  // 同一个item选中变未选中
                    for (MemberCardBean memberCardBean : memberCardList) {// 遍历list集合中的数据
                        memberCardBean.setSelect(false);// 全部设置为未选中
                    }
                    mSelectedPos = -1;
                    ((OrderConfirmActivity)context).tv_member_card_dikou.setText("请选择会员卡");
                    ((OrderConfirmActivity)context).memberCardId="";
                } else if (mSelectedPos != position) {  // 不是同一个item选中当前的， 去除上一个选中的状态
                    for (MemberCardBean memberCardBean : memberCardList) {// 遍历list集合中的数据
                        memberCardBean.setSelect(false);// 全部设置为未选中
                    }

                    memberCardList.get(position).setSelect(true);
                    mSelectedPos = position;
                }


                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return memberCardList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_check;
        private TextView tv_member_title;
        private TextView tv_price;
        private TextView tv_member_message;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_check = itemView.findViewById(R.id.iv_check);
            tv_member_message = itemView.findViewById(R.id.tv_member_message);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_member_title = itemView.findViewById(R.id.tv_member_title);
        }
    }
}
